import os
from PIL import Image
def augment_image(img_path, output_dir):
    """处理并增强图像"""
    if not os.path.isfile(img_path):  # 确保路径是文件
        print(f"Skipping {img_path}: Not a file.")
        return

    try:
        # 打开图像并转换为灰度图
        img = Image.open(img_path).convert("L")

        # 数据增强示例：保存图像到目标路径
        augmented_path = os.path.join(output_dir, os.path.basename(img_path))
        img.save(augmented_path)
    except Exception as e:
        print(f"Error processing {img_path}: {e}")


def process_directory(input_dir, output_dir):
    """递归处理整个文件夹"""
    for root, dirs, files in os.walk(input_dir):  # 遍历子文件夹和文件
        for file_name in files:
            file_path = os.path.join(root, file_name)  # 获取文件完整路径
            relative_dir = os.path.relpath(root, input_dir)  # 相对于输入目录的子路径
            target_dir = os.path.join(output_dir, relative_dir)  # 构建输出路径
            os.makedirs(target_dir, exist_ok=True)  # 确保目标文件夹存在
            augment_image(file_path, target_dir)


# 输入输出目录
input_dir = "raw"
output_dir = "processed"

# 处理整个 raw 文件夹
process_directory(input_dir, output_dir)